package approve.action;

import java.util.Timer;

import project.active.LogoutActivity;
import resource.pojo.Approvedoc;
import nguyen.dao.ReceiverDocumentDAO;
import model.pojo.Document;
import model.pojo.Employee;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.NotificationManager;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import approval.dao.ApproveDocumentDAO;

public class My_ViewForm_Lessthan10 extends Activity {
	// component
	TextView la_title, la_writer, la_department, la_content;
	// EditText title,writer,department;
	// WebView content;

	TextView title, writer, department, content;

	// bundle
	Approvedoc AppDoc = null;
	String database = "admin";
	String lang = "EN";
	Bundle language = null;
	Employee emp = new Employee();

	Timer timer = new Timer();
	NotificationManager mNotificationManager = null;
	private int SIMPLE_NOTFICATION_ID;
	TextView logout, username;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.my_viewform_lessthan10);
		getComponent();

		mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

		loadBundle();
		if (getIntent().getExtras().getString("delete") != null) {
			LinearLayout linearDelete = (LinearLayout) findViewById(R.id.linearDelete);
			Button btnDelete = new Button(this);
			btnDelete.setText(language.getString("Delete"));
			btnDelete.setOnClickListener(selectDelete);
			linearDelete.addView(btnDelete);
			linearDelete.setVisibility(View.VISIBLE);
		}
		logout = (TextView) findViewById(R.id.logout);
		username = (TextView) findViewById(R.id.username);

		logout.setOnClickListener(listenerLogout);
		username.setText(emp.getUserName());

		viewDocument();

	}

	public String getDatabase() {
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			database = extras.getString("database");
		}
		return database;
	}

	private OnClickListener listenerLogout = new OnClickListener() {

		public void onClick(View v) {
			// TODO Auto-generated method stub
			// Intent i=new Intent(MainFrame.this, LoginActivity.class);
			timer.cancel();
			mNotificationManager.cancel(SIMPLE_NOTFICATION_ID);

			Intent i = new Intent(My_ViewForm_Lessthan10.this,
					LogoutActivity.class);
			i.putExtra("database", getDatabase());
			startActivity(i);
		}
	};

	private void loadLanguage() {
		if (language != null) {
			la_title.setText(language.getString("title"));
			la_writer.setText(language.getString("writer"));
			la_department.setText(language.getString("Department"));
			la_content.setText(language.getString("content"));
		}
	}

	private void getComponent() {
		la_title = (TextView) findViewById(R.id.la_title);
		la_writer = (TextView) findViewById(R.id.la_writer);
		la_department = (TextView) findViewById(R.id.la_department);
		la_content = (TextView) findViewById(R.id.la_content);

		title = (TextView) findViewById(R.id.title);
		writer = (TextView) findViewById(R.id.writer);
		department = (TextView) findViewById(R.id.department);
		// content=(WebView) findViewById(R.id.content);
		content = (TextView) findViewById(R.id.content);
	}

	private void loadBundle() {
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			database = extras.getString("database");
			lang = extras.getString("lang");
			emp = (Employee) extras.get("em");
			AppDoc = (Approvedoc) extras.get("AppDoc");
			language = extras.getBundle(lang);
		}
	}

	private void viewDocument() {
		if (AppDoc != null) {
			title.setText(AppDoc.getTitle());

			String[] arr = ReceiverDocumentDAO.getDepartment_EmployeeName(
					AppDoc.getiDEmployee(), database, lang);
			if (arr != null && arr.length == 2) {
				writer.setText(arr[0]);
				department.setText(arr[1]);
			}

			// final String mimeType = "text/html";
			// String header = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>";
			// content.loadData(header+AppDoc.getContent(), mimeType, null);

			content.setText(Html.fromHtml(AppDoc.getContent()));
		}
	}

	OnClickListener selectDelete = new OnClickListener() {
		@Override
		public void onClick(View arg0) {
			dialogDelete();
		}
	};

	private void dialogDelete() {
		AlertDialog.Builder alert = new AlertDialog.Builder(this);
		alert.setTitle(language.getString("Delete"));
		alert.setMessage(language.getString("are-you-sure"));
		alert.setPositiveButton(language.getString("ok"),
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface arg0, int arg1) {
						ApproveDocumentDAO.moveApproveToTrash(AppDoc.getiDApproveDoc(),
								database);
						goToDeclineDocument();
					}
				});
		alert.setNegativeButton(language.getString("cancel"),
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface arg0, int arg1) {
					}
				});

		AlertDialog dialog = alert.create();
		dialog.show();
	}

	private void goToDeclineDocument() {
		finish();
		Intent intent = new Intent(My_ViewForm_Lessthan10.this,
				My_App_DeclineDocument.class);
		Bundle extras = getIntent().getExtras();
		try {
			extras.remove("delete");
			extras.remove("AppDocID");
		} catch (Exception e) {
		}
		intent.putExtras(extras);
		startActivity(intent);
	}
}
